<?php

namespace app\api\controller\fruit\v22;

use app\api\model\fruit\warrior\Achieve;
use app\api\model\fruit\warrior\User;
use app\api\controller\fruit\BaseWarrior;
use think\cache\driver\Redis;
use think\Request;

/**
 * @author zero
 * @Time 2019/3/2
 *
 * Class Warrior
 * @package app\api\controller\fruit\v22
 *
 * 切水果勇士基类 v22
 */
class Warrior extends BaseWarrior
{

    protected $cache = false; // json文件缓存

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->_initialize();
    }

    // 初始化
    protected function _initialize()
    {
        //移除HTML标签
        $this->request->filter('strip_tags');
        $actionname = strtolower($this->request->action());
        $token = $this->request->server('HTTP_TOKEN', $this->request->request('token')) ?: '3ab0f3dee498e487c416213ac0c09885';
        $this->user = User::get(['token' => $token]);
        // 检测是否需要验证登录
        if (!$this->match($this->noNeedLogin, $actionname)) {
            //检测是否登录
            if (!$this->user) {
                $this->error(__('Please login first'), null, 401);
            }
        }
    }

    // 获取游戏常量
    public function get_game_const()
    {
        return $this->get_json_file('v22_fruit_warrior_game_const', '/fruit/warrior/v22/GameConstDefine', $this->cache);
    }

    // 获取签到条件
    public function get_sign_condition()
    {
        return $this->get_json_file('v22_fruit_warrior_sign_in', '/fruit/warrior/v22/signDefine', $this->cache);
    }

    // 获取武器列表
    public function get_weapon_define()
    {
        return $this->get_json_file('v22_fruit_warrior_weapon_define', '/fruit/warrior/v22/weaponDefine', $this->cache);
    }

    // 获取道具列表
    public function get_item_define()
    {
        return $this->get_json_file('v22_fruit_warrior_item_define', '/fruit/warrior/v22/itemDefine', $this->cache);
    }

    // 获取武器商店数据
    public function get_weapon_mall()
    {
        return $this->get_json_file('v22_fruit_warrior_weapon_mall', '/fruit/warrior/v22/weaponMall', $this->cache);
    }

    // 获取武器商店数据
    public function get_achieve_define()
    {
        return $this->get_json_file('v22_fruit_warrior_achieve_define', '/fruit/warrior/v22/achievementDefine', $this->cache);
    }

    // 处理扣费
    public function handle_cost($con)
    {
        $arr = explode('_', $con);
        $type = intval($arr[0]);
        $cost = intval($arr[1]);
        if ($type == 1) {
            if ($this->user->coin >= $cost) {
                $this->user->coin -= $cost;
                return true;
            }
        } elseif ($type == 2) {
            if ($this->user->diamond >= $cost) {
                $this->user->diamond -= $cost;
                return true;
            }
        }
        return false;
    }

    // 更新用户奖励
    public function handle_award($award_str, $multiple = 1)
    {
        $arr = explode('_', $award_str);
        $award_head = $arr[0];
        $award_index = intval($arr[1]);
        $award_count = intval($arr[2]);
        $multiple = intval($multiple);

        if ($award_index == 1) { // 加金币
            $award_count = $award_count * $multiple;
            $this->user->coin += $award_count;
        } elseif ($award_index == 2) { // 加钻石
            $award_count = $award_count * $multiple;
            $this->user->diamond += $award_count;
        } elseif ($award_index > 4) { // 加武器
            $item_define = $this->get_item_define();
            $item = $item_define->$award_index;
            if (!in_array($item->relatedId, explode(',', $this->user->weapon_list_v2))) {
                $this->user->weapon_list_v2 = $this->user->weapon_list_v2 . ',' . $item->relatedId;
            }
        }
        return $award_head . '_' . $award_index . '_' . $award_count;
    }

    // 计算离线收益
    public function handle_offline_income()
    {
        $diamond = 0;
        if ($this->user->last_logout_time) {
            $offtime = floor((intval(time()) - $this->user->last_logout_time) / 3600);
            if ($offtime >= 1 && $offtime <= 2) {
                $diamond = 20;
            } elseif ($offtime > 2 && $offtime <= 4) {
                $diamond = 40;
            } elseif ($offtime > 4 && $offtime <= 8) {
                $diamond = 60;
            } elseif ($offtime > 8 && $offtime <= 12) {
                $diamond = 80;
            } elseif ($offtime > 12) {
                $diamond = 100;
            }
        }
        return $diamond;
    }

    // 根据武器id获取对应道具id
    public function get_item_by_weapon($weapon_id)
    {
        $item_define = $this->get_item_define();
        $_id = $weapon_id + 4; // 由于item与武器差4个,所以加上
        $item = $item_define->$_id;
        // 判断是否正确,否则遍历取出
        if ($item && $item->relatedId == $weapon_id) {
            return $item->id;
        } else {
            foreach ($item_define as $item) {
                if ($item && $item->relatedId == $weapon_id) {
                    return $item->id;
                }
            }
        }
    }

    // 获取是否有未完成成就
    public function achieve($achieve = null)
    {
        $achieve = is_null($achieve) ? Achieve::get(['user_id' => $this->user->id]) : $achieve;
        if ($achieve) {
            $achieve = $achieve->toArray();
            foreach ($achieve as $value) {
                if ($value == 'complete') {
                    return true;
                }
            }
        } else {
            $achieves = new Achieve();
            $achieves->user_id = $this->user->id;
            $achieves->openid = $this->user->openid;
            $achieves->level = $this->user->level;
            $achieves->login_num = 1;
            $achieves->save();
        }
        return false;
    }

    // 处理客户端传来的分享获取视频成就
    public function handle_client_achieve()
    {
        $type = $this->request->post('type') ?: null;
        if ($type) {
            $this->handle_achieve($type);
        }
        $this->result('ok');
    }

    // 处理成就
    public function handle_achieve($type)
    {
        $achieves = Achieve::get(['user_id' => $this->user->id]);
        if ($achieves) {
            if ($this->handle_add_one_achieve($achieves, $type)) {
                if ($achieves->openid == null) {
                    $achieves->openid = $this->user->openid;
                }
                $achieves->save();
            }
        } else {
            $achieves = new Achieve();
            $achieves->user_id = $this->user->id;
            $achieves->openid = $this->user->openid;
            $achieves->level = $this->user->level;
            $achieves->$type = 1;
            $achieves->save();
        }
        return;
    }

    // 处理加一成就
    public function handle_add_one_achieve($achieves, $type)
    {
        if ($achieves->$type != 'complete' && $achieves->$type != 'get') {
            $achieves->$type += 1;
            return true;
        } else {
            return false;
        }
    }

    // 处理成就条件
    public function handle_achieve_condition($achieve, $str)
    {
        $arr = explode('_', $str);
        $condition = intval($arr[0]);
        switch ($condition) {
            case 3:
                return '3_' . $this->handle_achieve_complete($achieve, 'level', intval($arr[1]));
            case 6:
                return '6_' . $this->handle_achieve_complete($achieve, 'share_num', intval($arr[1]));
            case 7:
                return '7_' . $this->handle_achieve_complete($achieve, 'video_num', intval($arr[1]));
            case 8:
                return '8_' . $this->handle_achieve_complete($achieve, 'login_num', intval($arr[1]));
            case 9:
                $num = $this->handle_achieve_complete($achieve, 'weapon_' . intval($arr[1]), intval($arr[2]));
                return '9_' . intval($arr[1]) . '_' . $num;
            case 10:
                $num = $this->handle_achieve_complete($achieve, 'fruit_' . intval($arr[1]), intval($arr[2]));
                return '10_' . intval($arr[1]) . '_' . $num;
            case 11:
                return '11_' . $this->handle_achieve_complete($achieve, 'one_cut_num', intval($arr[1]));
            case 12:
                return '12_' . $this->handle_achieve_complete($achieve, 'lucky_wheel_num', intval($arr[1]));
        }
    }

    // 处理条件是否完成
    public function handle_achieve_complete($achieve, $type, $num)
    {
        if ($achieve->$type != 'complete' && $achieve->$type != 'get') {
            if ($achieve->$type >= $num) {
                $achieve->$type = 'complete';
            }
        }
        return $achieve->$type;
    }

    // 获取当前试用的武器
    public function get_use_exp_weapon()
    {
        $redis = new Redis();
        if ($redis->get($this->user->openid . '_fruit_warrior_use_exp_weapon')) {
            $experience = $redis->get($this->user->openid . '_fruit_warrior_exp_weapon');
            if ($experience) {
                return $experience;
            }
        }
        return null;
    }

}
